package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.model.Label;
import com.tensquare.util.IdWorker;
import com.tensquare.util.NotNullHelps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.LinkedList;
import java.util.List;

@Service
public class LabelService {

    @Autowired
    private LabelDao labelDao ;

    @Autowired
    private IdWorker idWorker ;

    public void deleteById(String labelId) {
        labelDao.deleteById(labelId);
    }


    public void updateLabel(Label label){
        labelDao.save(label);
    }

    public void saveLabel(Label label){
        label.setId(idWorker.nextId().toString());
        labelDao.save(label);
    }

    public List<Label> findAll() {
        return labelDao.findAll();
    }

    public Label findById(String labelId) {
        return labelDao.findById(labelId).get();
    }


    public List<Label> searchLabel(Label condition) {
        List<Label> labelList = null ;
        labelList = labelDao.findAll(new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> predicateList = new LinkedList<Predicate>();
                String labelName = condition.getLabelname();
                if (NotNullHelps.isNotNull(labelName)) {
                    Predicate predicate = builder.like(root.get("labelname").as(String.class), "%" + labelName + "%");
                    predicateList.add(predicate);
                }
                String state = condition.getState();
                if (NotNullHelps.isNotNull(state)) {
                    Predicate predicate = builder.equal(root.get("state").as(String.class), state);
                    predicateList.add(predicate);
                }
                Predicate[] predicateArray = new Predicate[predicateList.size()];
                predicateArray = predicateList.toArray(predicateArray);
                return builder.and(predicateArray);
            }
        });
        return labelList;
    }

    public Page<Label> searchLabelByPage(Label condition, Integer page, Integer size) {
        Specification<Label> params = new Specification<Label>(){
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> predicateList = new LinkedList<Predicate>();
                String state = condition.getState();
                if (NotNullHelps.isNotNull(state)) {
                    Predicate predicate = builder.equal(root.get("state").as(String.class), state);
                    predicateList.add(predicate);
                }
                String labelName = condition.getLabelname();
                if (NotNullHelps.isNotNull(labelName)) {
                    Predicate predicate = builder.like(root.get("labelname").as(String.class), "%" + labelName + "%");
                    predicateList.add(predicate);
                }
                Predicate[] predicateArray = new Predicate[predicateList.size()];
                predicateArray = predicateList.toArray(predicateArray);
                return builder.and(predicateArray);
            }
        };
        Pageable pageAble = PageRequest.of(page - 1, size);
        return labelDao.findAll(params, pageAble);
    }

}
